<?php
/**
 * Copyright (c) 2006, PORTALIER Julien
 * 
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 * 
 * @package      FeatherPHP
 * @subpackage   Libs
 * @copyright    Copyright (c) 2006, Julien PORTALIER
 * @link         http://feather.portalier.com
 * @license      http://www.opensource.org/licenses/mit-license.php The MIT License
 */

require APP.'config'.DS.'inflections.php';

class Inflector extends Inflections
{
	static function camelize($str)
	{
		return str_replace(' ', '', ucwords(str_replace('_', ' ', $str)));
	}
	
	static function underscore($str)
	{
		return str_replace(' ', '_', strtolower(preg_replace('/(?<=\w)([A-Z])/', ' \1', $str)));
	}
	
	static function tableize($str)
	{
		return self::underscore(self::pluralize($str));
	}
	
	static function classify($str)
	{
		return self::camelize(self::singularize($str));
	}
	
	static function humanize($str)
	{
		return str_replace('_', ' ', $str);
	}
	
	static function variablize($str)
	{
		$str    = self::camelize($str);
		$str{0} = strtolower($str{0});
		return $str;
	}
	
	static function singularize($str)
	{
		if (in_array(strtolower($str), self::$constants))
			return $str;
		foreach(self::$singularize_rules as $rule => $value)
		{
			if (preg_match($rule, $str))
				return preg_replace($rule, $value, $str);
		}
		return $str;
	}
	
	static function pluralize($str)
	{
		if (in_array(strtolower($str), self::$constants))
			return $str;
		foreach(self::$pluralize_rules as $rule => $value)
		{
			if (preg_match($rule, $str))
				return preg_replace($rule, $value, $str);
		}
		return $str;
	}
}
?>